package com.gmail.blackdog1987.onedollar.dao;

import com.gmail.blackdog1987.onedollar.dao.hibernate.EntityHibernateDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.BaseJdbcDao;
import com.gmail.blackdog1987.onedollar.dao.hibernate.jdbc.JdbcUtil;
import com.gmail.blackdog1987.onedollar.entity.TGoodsShowEntity;
import com.gmail.blackdog1987.onedollar.entity.TNoticeEntity;
import com.gmail.blackdog1987.onedollar.entity.TUserEntity;
import com.gmail.blackdog1987.onedollar.entity.base.Page;
import com.gmail.blackdog1987.onedollar.vo.GoodsOneDollarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository("noticeDao")
public class NoticeDao extends EntityHibernateDao<TNoticeEntity>{
    @Autowired
    private BaseJdbcDao baseJdbcDao;

    public Page<TNoticeEntity> queryForPage(int pageindex, int pagesize){
        String whereSql = " FROM t_notice ORDER BY createtime DESC";

        String selectionSql = " SELECT * ";

        String limit = " limit " + (pageindex-1)*pagesize + "," + pagesize;
        Integer totalCount = baseJdbcDao.getJdbcTemplate().queryForInt(" SELECT COUNT(*) " + whereSql , null);

        List<TNoticeEntity> list = JdbcUtil.queryForList(baseJdbcDao, TNoticeEntity.class, selectionSql + whereSql + limit, null);

        Page page = new Page(list, pageindex, totalCount, pagesize);

        return page;
    }

    public void updateRead(int id){
        String sql = " update t_notice set times = times+1 where id = ?";
        baseJdbcDao.getJdbcTemplate().update(sql, new Object[]{id});
    }
}
